package com.example.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.util.Random;
@WebServlet("/GenerateCodeServlet")
public class GenerateCodeServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        // 生成6位随机验证码
        Random random = new Random();
        int code = random.nextInt(899999) + 100000; // 生成6位数字验证码

        // 将验证码设置到session中，以便后续验证
        request.getSession().setAttribute("verificationCode", String.valueOf(code));

        // 构建响应消息
//        String jsonResponse = "{\"message\": \"验证码已发送，请查收。\"}";
//        构建一个包含验证码的JSON字符串
        response.getWriter().write("{\"message\": \"" + code + "\"}");
        // 发送响应
//        response.getWriter().write(jsonResponse);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
